/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             BaseExportCSV.cs
*        创建系统时间:       2012/11/2 19:07:07
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Windows.Forms;

    public class BaseExportCSV
    {
        public static void ExportCSV(DataSet dataSet, string fileName)
        {
            StreamWriter writer = new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312"));
            writer.WriteLine(GetCSVFormatData(dataSet).ToString());
            writer.Flush();
            writer.Close();
        }

        public static void ExportCSV(DataTable dataTable, string fileName)
        {
            StreamWriter writer = new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312"));
            writer.WriteLine(GetCSVFormatData(dataTable).ToString());
            writer.Flush();
            writer.Close();
        }

        public static void ExportCSV(DataGridView DataGridView, string fileName)
        {
            StreamWriter writer = new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312"));
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < DataGridView.Columns.Count; i++)
            {
                if (DataGridView.Columns[i].Visible && (DataGridView.Columns[i].DataPropertyName.ToUpper() != BaseBusinessLogic.SelectedColumn.ToUpper()))
                {
                    builder.Append(DataGridView.Columns[i].HeaderText + ",");
                }
            }
            writer.WriteLine(builder.ToString());
            foreach (DataRowView view in (DataView)DataGridView.DataSource)
            {
                builder = new StringBuilder();
                for (int j = 0; j < DataGridView.Columns.Count; j++)
                {
                    if (DataGridView.Columns[j].Visible && (DataGridView.Columns[j].DataPropertyName.ToUpper() != BaseBusinessLogic.SelectedColumn.ToUpper()))
                    {
                        builder.Append(view[DataGridView.Columns[j].DataPropertyName].ToString() + ",");
                    }
                }
                writer.WriteLine(builder.ToString());
            }
            writer.Flush();
            writer.Close();
        }

        public static StringBuilder GetCSVFormatData(DataSet dataSet)
        {
            StringBuilder builder = new StringBuilder();
            foreach (DataTable table in dataSet.Tables)
            {
                builder.Append(GetCSVFormatData(table));
            }
            return builder;
        }

        public static StringBuilder GetCSVFormatData(DataTable dataTable)
        {
            StringBuilder builder = new StringBuilder();
            foreach (DataColumn column in dataTable.Columns)
            {
                builder.Append(column.ColumnName.ToString() + ",");
            }
            builder.Append("\n");
            foreach (DataRowView view in dataTable.DefaultView)
            {
                foreach (DataColumn column2 in dataTable.Columns)
                {
                    builder.Append(view[column2.ColumnName].ToString() + ",");
                }
                builder.Append("\n");
            }
            return builder;
        }

        public static void GetResponseCSV(DataSet dataSet, string fileName)
        {
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.Write(GetCSVFormatData(dataSet).ToString());
            HttpContext.Current.Response.End();
        }

        public static void GetResponseCSV(DataTable dataTable, string fileName)
        {
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.Write(GetCSVFormatData(dataTable).ToString());
            HttpContext.Current.Response.End();
        }
    }
}